package com.aiyige.utils;

import android.content.Context;
import android.text.TextUtils;

import com.vondear.rxtools.RxFileTool;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;

import eu.medsea.mimeutil.MimeType;
import eu.medsea.mimeutil.MimeUtil;

/**
 * Created by zhuyakun on 2017/11/8.
 */

public class FileUtil {
    public static String getFileName(String path) {
        String fileName = RxFileTool.getFileName(path);
        String name = "";
        if (TextUtils.isEmpty(fileName)) {
            return "";
        }
        if (fileName.contains(".")) {
            String names[] = fileName.split("\\.");
            if (names != null && names.length > 0) {
                name = names[0];
            }
        }
        return name;

    }

    public static long getFileSize(String path) {
        try {
            return new File(path).length();
        } catch (Exception e) {
            return 0;
        }
    }

    public static String detectFileExtension(String path) {
        try {
            File file = new File(path);
            MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
            Collection<?> mimeTypes = MimeUtil.getMimeTypes(file);
            MimeType mimeType = MimeUtil.getFirstMimeType(mimeTypes.toString());
            return "." + mimeType.getSubType();
        } catch (Exception e) {
            return "";
        }
    }


    private static final String SEPARATOR = File.separator;//路径分隔符

    public static void copyFilesFromRaw(Context context, int id, String fileName, String storagePath){
        InputStream inputStream=context.getResources().openRawResource(id);
        File file = new File(storagePath);
        if (!file.exists()) {//如果文件夹不存在，则创建新的文件夹
            file.mkdirs();
        }
        readInputStream(storagePath + SEPARATOR + fileName, inputStream);
    }

    /**
     * 读取输入流中的数据写入输出流
     *
     * @param storagePath 目标文件路径
     * @param inputStream 输入流
     */
    public static void readInputStream(String storagePath, InputStream inputStream) {
        File file = new File(storagePath);
        try {
            if (!file.exists()) {
                // 1.建立通道对象
                FileOutputStream fos = new FileOutputStream(file);
                // 2.定义存储空间
                byte[] buffer = new byte[inputStream.available()];
                // 3.开始读文件
                int lenght = 0;
                while ((lenght = inputStream.read(buffer)) != -1) {// 循环从输入流读取buffer字节
                    // 将Buffer中的数据写到outputStream对象中
                    fos.write(buffer, 0, lenght);
                }
                fos.flush();// 刷新缓冲区
                // 4.关闭流
                fos.close();
                inputStream.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}
